package io.renren.modules.dangan.controller;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.alicp.jetcache.Cache;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.CreateCache;
import io.renren.common.annotation.SysLog;
import io.renren.common.utils.R;
import io.renren.modules.dangan.service.impl.FileServiceImpl;
import io.renren.modules.dangan.utils.FileUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @auther guqianbin
 * @date 2019/8/4 21:17
 **/
@Controller
@RequestMapping("/file")
@CrossOrigin
public class FileController {

    @Autowired
    private FileServiceImpl fileService;

    @Value("${chromePath}")
    private String chromeFilePath;
    @CreateCache(name="tempFile",cacheType= CacheType.LOCAL,localExpire = 1800,localLimit = 100)
    private Cache<String,String> uploadFileTemp;
    @RequestMapping("/downloadChrome")
    public void downloadChrome( HttpServletResponse response){
        try {
            FileUtils.writerFile(response, new File(chromeFilePath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    @SysLog("查看档案pdf文件")
    @RequestMapping("/download/{jnmlbh}")
    public void getFile(@PathVariable("jnmlbh") String  jnmlbh, HttpServletResponse response, HttpServletRequest request) throws IOException {
        fileService.getFile(jnmlbh, response, request);
    }
    @RequestMapping("/getOaWsFile/{vrecid}")
    public void getOaWsFile(@PathVariable("vrecid") Integer vrecid
            , HttpServletResponse response) throws IOException {
        fileService.getOaWsFile(vrecid,response);
    }
    @RequestMapping("/{fileUUID}")
    public void getFileByPath(@PathVariable("fileUUID") String fileUUID
            , HttpServletResponse response)throws IOException {
        fileService.getFileByPath(fileUUID,response);
    }

    @RequestMapping("/upload")
    @ResponseBody
    public R uploadFile(HttpServletRequest request){
        R r=R.ok();
        try {
            // 解析器解析request的上下文
            CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
            // 先判断request中是否包含mutipart类型的数据
            if(multipartResolver.isMultipart(request)){
                // 再将request中得数据转化成multipart类型的数据
                MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
                List<MultipartFile> multipartFiles= multiRequest.getFiles("file");
                Iterator iterator= multiRequest.getFileNames();
                while(iterator.hasNext()){
                    MultipartFile file = multiRequest.getFile((String) iterator.next());
                    String filePath=fileService.uploadFile(file);
                    String uuid=IdUtil.fastUUID();
                    r.put("filePath",filePath);
                    r.put("uuid",uuid);
                    uploadFileTemp.put(uuid, filePath);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  r;
    }
}
